/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Dialog = require("dialog");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var activeUser = store.get("user");
	
    module.exports = {
    		subnav: {
    			saveBackCard: function(widget){
    			var backCardData = {};
    			var form = widget.get("form");
    			var pkTravelContract = form.getValue("pkTravelContract");
    			backCardData.pkReturnCredentials = form.getValue("pkReturnCredentials");
    			backCardData.operator = form.getValue("operator");
    			backCardData.cause = form.getValue("cause");
    			backCardData.defaultRatio = form.getValue("defaultRatio");
    			backCardData.flatCost = form.getValue("flatCost");
    			backCardData.price = form.getValue("price");
    			backCardData.operateDate = form.getValue("operateDate");
    			backCardData.version = form.getValue("version");
    			backCardData.flowStatus = "Initial";
    			var dat = aw.customParam(backCardData)+"&travelContract.pkTravelContract="+pkTravelContract+"&travelContract.version="+form.getValue("contractVersion");
    			
    			aw.saveOrUpdate("api/returncredentials/save",dat,function(datas){
    				widget.hide([".J-form",".J-coupongrid",".J-applicategrid"]).show([".J-grid"]);
        			widget.get("subnav").hide(["return","save"]).show(["status","startDate","search","add"]);
        			widget.get("grid").refresh();
    			})
    		}
    },
    grid: {
    	statusChange: function(widget,pkReturnCredentials){
    		widget.service.statusChange({
    			pkReturnCredentials:pkReturnCredentials,
    			//version:version,
    			fetchProperties: widget.constant.fetchProperties
    		},function(data){
    			var grid = widget.get("grid");
    			grid.refresh();
    		})
    	}
    },
    form: {
    	searchContact:function(widget,memberCertificates,pkReturnCredentials,flag){
    		var fetchProperties =  "travelContract.pkTravelContract,travelContract.contactNo,travelContract.status.*,travelContract.unitPrice,travelContract.productRule.product.pkProduct,travelContract.version," +
				"travelContract.balTime,travelContract.unit.*,travelContract.productRule.product.name,travelContract.productType.pkProductType,travelContract.productType.name," +
   				"travelContract.number,travelContract.price,travelContract.balance,travelContract.signUpDate,travelContract.member.personalInfo.pkPersonalInfo," +
   				"travelContract.member.personalInfo.name,travelContract.member.personalInfo.idNumber,travelContract.member.personalInfo.mobilePhone," +
   				"welfareccList.pkWelfarecc,welfareccList.cardNo,welfareccList.count,welfareccList.returnCertificateTotal,welfareccList.consumptionTotal,welfareccList.cashTotal,welfareccList.balance," +
   				"appList.sequenceNumber,appList.checkTime,appList.note,appList.checker.pkUser,appList.checker.name,appList.status.*"
   			widget.service.queryTravelcontract({
				memberCertificates:memberCertificates,
				//contactNo:contactNo,
				pkReturnCredentials: pkReturnCredentials,
				flag:flag,
           		fetchProperties:fetchProperties,
			},function(data){
				var form = widget.get("form");
				form.setValue("contractVersion",data.travelContract.version);
				form.setValue("pkTravelContract",data.travelContract.pkTravelContract);
				form.setValue("contactNo",data.travelContract.contactNo);
				form.setValue("status.value",data.travelContract.status.value);
				form.setValue("product.name",data.travelContract.productRule.product.name);
				form.setValue("productType.name",data.travelContract.productType.name);
				form.setValue("number",data.travelContract.number);
				form.setValue("totalPrice",data.travelContract.price);
				form.setValue("signUpDate",data.travelContract.signUpDate);
				form.setValue("balance",data.travelContract.balance == null ? 0 : data.travelContract.balance);
				form.setValue("name",data.travelContract.member.personalInfo.name);
				form.setValue("idNumber",data.travelContract.member.personalInfo.idNumber);
				form.setValue("phone",data.travelContract.member.personalInfo.mobilePhone);
				if(data.travelContract.unit != null && data.travelContract.unit.key == "Day"){
					form.setValue("balTime",data.travelContract.balTime.toFixed(0));
					form.setValue("balance",(data.travelContract.balTime || 0)*(data.travelContract.unitPrice || 0));
				}else{
					form.setValue("balance",data.travelContract.balance == null ? 0 : data.travelContract.balance);
				}
				if(data.travelContract.unit != null){
					form.setValue("unit",data.travelContract.unit.value);
					form.setValue("balTime",data.travelContract.balTime.toFixed(0));
				}
				var coupongrid = widget.get("coupongrid");
				coupongrid.setData(data.welfareccList);
				
				
				if(pkReturnCredentials != null){
					//form.setDisabled(true);
					/*var coupongrid = widget.get("coupongrid");
					widget.hide([".J-grid"]).show([".J-form",".J-coupongrid",".J-applicategrid"]);
					widget.get("subnav").show(["return"]).hide(["status","startDate","search","add","save"]);*/
					var applicategrid = widget.get("applicategrid");
					applicategrid.setData(data.appList);
				}
 			})
		}
    }
    };
});